package cn.com.ctbri.geomonitor.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebParam.Mode;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.config.authentication.UserServiceBeanDefinitionParser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.com.ctbri.geomonitor.bean.UserVO;
import cn.com.ctbri.geomonitor.model.Flow;
import cn.com.ctbri.geomonitor.model.FlowName;
import cn.com.ctbri.geomonitor.service.GroupRestService;
import cn.com.ctbri.geomonitor.service.UserRestService;
import cn.com.ctbri.geomonitor.utils.JsonListUtil;
import cn.com.ctbri.geomonitor.utils.SystemConstants;
import net.sf.json.JSONObject;

import org.activiti.engine.identity.User;
import org.activiti.engine.identity.Group;

@RestController
public class UserRestController {
	@Resource(name = "userRestService")
	UserRestService userRestService;
	@Resource(name = "groupRestService")
	GroupRestService groupRestService;

	// 用户登录
	@ResponseBody
	@RequestMapping(value = "user/login", method = RequestMethod.POST, produces = "text/html; charset=utf-8")
	public String userLogin(@RequestParam("username") String username, @RequestParam("password") String password,
			@RequestParam("loginRole") String loginRole, HttpSession session) {

	

		User loginUser = userRestService.userLogin(username, password);
		
		if (loginUser == null) {
			return "用户名或密码错误！";
		} else {
			Group group = userRestService.getGroupID(loginUser.getId());
			if(!group.getType().equals(loginRole)){
				return "用户角色与用户不匹配！";
			}
			// 写入session记录登录状态
			session.setAttribute(SystemConstants.SESSION_USER, loginUser);
			session.setAttribute(SystemConstants.SESSION_GROUP, group);
			return "登录成功！";
		}
	}

	// 用户注册
	@ResponseBody
	@RequestMapping(value = "user/register", method = RequestMethod.POST, produces = "text/html; charset=utf-8")
	public String userRegister(@RequestParam("username") String username, @RequestParam("password") String password,
			@RequestParam("role") String role, HttpSession session) {

		System.out.println(role);

		if (username == null) {
			return "用户名为空！";
		}
		if (password == null) {
			return "密码为空！";
		}
		User user = userRestService.checkUserName(username);
		if (user == null) {
			// 添加用户
			userRestService.register(username, password, role);
			User user1 = userRestService.checkUserName(username);
			if (user1 != null) {
				return "注册成功，请登录！";
			} else {
				return "注册失败";
			}

		} else {
			return "用户，名已存在！";
		}

	}

	// 页面重定向
	@RequestMapping({ "/index" })
	public ModelAndView indexCore(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("login");
		return mv;
	}

	// 退出登录
	@RequestMapping({ "/user/logout" })
	public ModelAndView logout(HttpSession httpSession) {
		ModelAndView mv = new ModelAndView();
		httpSession.removeAttribute(SystemConstants.SESSION_USER);
		mv.setViewName("login");
		return mv;
	}

	// 查询所有用户
	@RequestMapping({ "user/listAllUsers" })
	public ModelAndView listAllUsers() {
		ModelAndView mv = new ModelAndView();
		List<UserVO> users = userRestService.getAllUsers();
		List<Group> groups = groupRestService.list();
		mv.addObject("groups", groups);
		mv.addObject("users", users);
		mv.setViewName("user");
		return mv;
	}

	

	// 添加用户
	@RequestMapping(value = "user/addUser", method = RequestMethod.POST, produces = "text/html; charset=utf-8")
	public ModelAndView addUser(UserVO userForm) {
		// 保存一个用户
		userRestService.saveUser(userForm);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user");
		return mv;
	}

	// 读取用户流程申请材料清单
	@RequestMapping(value = "user/getMaterial", method = RequestMethod.POST, produces = "text/html; charset=utf-8")
	public String getMaterial(@RequestParam("flowId") String flowId, @RequestParam("flowName") String flowName,HttpSession session){
		
		List<Flow> materialList = userRestService.getMaterial(flowId);
		session.setAttribute("materialList", materialList);
		session.setAttribute("flowName", flowName);
		return "material";
	}
	
	/*public List<Flow> getMaterial(@RequestParam("flowId") String flowId) {
		List<Flow> list = userRestService.getMaterial(flowId);
		return list;
	}*/

	// 读取用户所有流程
	@RequestMapping(value = "user/getAllFlow", method = RequestMethod.GET)
	public ModelAndView getAllFlow(HttpSession session){
		List<FlowName> flowList = userRestService.getAllFlow();
		session.setAttribute("flowList", flowList);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("getAllFlow");
		return mv;
	}
	@RequestMapping(value = "user/getNames")
	public ModelAndView getAllFlows() {
		List<FlowName> list = userRestService.getAllFlow();
		ModelAndView mv = new ModelAndView();
		mv.addObject("list", list);
		mv.setViewName("historyTask");
		return mv;
	}
//修改密码
	@RequestMapping(value = "user/updataPassword")
	public String updatePassword(@RequestParam("userId") String userId,@RequestParam("newPassword" ) String newPassword){
		ModelAndView modelAndView = new ModelAndView();
		
		String result = userRestService.updataPassword(userId, newPassword);
		
		return result;
	}
	//删除用户
	@RequestMapping(value = "user/deleteUser")
	public ModelAndView  deleteUser(@RequestParam("userId") String userId){
		ModelAndView modelAndView = new ModelAndView();
		String result = userRestService.delete(userId);
	
		return listAllUsers();
	}
}
